<?php
	require_lib('db/PDODB.class');
	class MutilPDODB extends PDODB{
		/*
		private $host = array(
			'read_host'=> array(
				array('host'=>'192.168.175.200', 'user'=>'root',  'pwd'=>'', 'port'=>3307),
				array('host'=>'192.168.175.221', 'user'=>'root',  'pwd'=>'', 'port'=>3306)
			),
			'write_host'=>array(
				array('host'=>'192.168.175.200', 'user'=>'root',  'pwd'=>'', 'port'=>3307),
				array('host'=>'192.168.175.221', 'user'=>'root',  'pwd'=>'', 'port'=>3306)
			)
		);
		*/
		private $host = array();
		function __construct(array $dbconf){
			parent::__construct($dbconf);
		}
		protected function get_host($readonly,$db,& $host,& $user,& $pwd, &$port){
			$context = $GLOBALS['context'];
			$host = $context->mutil_db_cache->get('mutil_db_conf');
			if ($host) {
				$this->host = $host;
			} else {
				$this->host = $context->get_app_conf('db_conf');
				$context->mutil_db_cache->set('mutil_db_conf',$this->host);
			}

			if ($readonly) {
				if (isset($this->host['read_host']) && !empty($this->host['read_host'])) {
					$idx = mt_rand(0, count($this->host['read_host']));
					$idx = $idx == count($this->host['read_host']) ? count($this->host['read_host']) -1 : $idx;
					$host = $this->host['read_host'][$idx]['host'];
					$user = $this->host['read_host'][$idx]['user'];
					$pwd = $this->host['read_host'][$idx]['pwd'];
					$port = $this->host['read_host'][$idx]['port'];
				} else {
					$host = $context->get_app_conf('db_host');
					$user = $context->get_app_conf('db_user');
					$pwd = $context->get_app_conf('db_pass');
					$port = $context->get_app_conf('db_port')? $context->get_app_conf('db_port') : 3306;
				}
			} else {
				if (isset($this->host['write_host']) && !empty($this->host['write_host'])) {
					$idx = mt_rand(0, count($this->host['write_host']));
					$idx = $idx == count($this->host['write_host']) ? count($this->host['write_host']) -1 : $idx;
					$host = $this->host['write_host'][$idx]['host'];
					$user = $this->host['write_host'][$idx]['user'];
					$pwd = $this->host['write_host'][$idx]['pwd'];
					$port = $this->host['write_host'][$idx]['port'];
				} else {
					$host = $context->get_app_conf('db_host');
					$user = $context->get_app_conf('db_user');
					$pwd = $context->get_app_conf('db_pass');
					$port = $context->get_app_conf('db_port')? $context->get_app_conf('db_port') : 3306;
				}
			}
			//echo "{$host};{$this->sel_db}@{$user}@{$pwd}@{$port}<br>";
		}
		static function register($prop) {
			$context=$GLOBALS['context'];
			$db_type=$context->get_app_conf('db_server'.$prop);
			$db_host=$context->get_app_conf('db_host'.$prop);
			$db_user=$context->get_app_conf('db_user'.$prop);
			$db_pass=$context->get_app_conf('db_pass'.$prop);
			$db_name=$context->get_app_conf('db_name'.$prop);
			$conf=array('type'=>$db_type,'host'=>$db_host,'user'=>$db_user, 'pwd'=>$db_pass, 'name'=>$db_name);
			$db_port=$context->get_app_conf('db_port'.$prop);
			if(isset($db_port))	$conf['port']=$db_port;
			$db = new MutilPDODB($conf);
			//$context->log_debug ( "pdodb object [type,host,db,user,port] create:{$db_type},{$db_host},{$db_name},{$db_user}, {$db_port}" );
			return $db;
		}
	}
